﻿#pragma once

//5个子线程，每个子线程负责一个tabwidget中数据的获取
//当前显示的是哪个tabwidget，ui界面去显示对应子线程获取的最新数据，其它的tabwidget对应的线程


#include <QtWidgets/QWidget>
#include <QThread>
#include <QTimer>
#include <QInputDialog>
#include <QCloseEvent>
#include <QFileDialog>
#include <QMutex>

#include"Communicator.h"
#include "GlobalDefine.h"
#include "VerifyDialog.h"
#include "MoreInfoDialog.h"
#include "ui_LiBatteryManagerSys.h"

QT_BEGIN_NAMESPACE
namespace Ui { class LiBatteryManagerSysClass; };
QT_END_NAMESPACE

class LiBatteryManagerSys : public QWidget
{
    Q_OBJECT

public:
    LiBatteryManagerSys(QWidget *parent = nullptr);
    ~LiBatteryManagerSys();
	quint16 calculateCRC16(const QByteArray &data);



signals:
	void sendSerialPort(SerialPortInfo info);
	void sendLanguageType(bool isChinese);
public slots:
	void on_pushButton_connect_clicked();
	//void on_pushButton_adjustProtect_clicked();
	void on_pushButton_CHG_CMD_Close_clicked();
	void on_pushButton_CHG_CMD_Open_clicked();
	void on_pushButton_DSG_CMD_Close_clicked();
	void on_pushButton_DSG_CMD_Open_clicked();
	void on_pushButton_RESET_CMD_clicked();
	void on_pushButton_ERASEFLASH_CMD_clicked();
	void on_pushButton_downsend_clicked();
	void on_toolButton_select_clicked();
	void on_pushButton_confirmKey();
	void on_pushButton_more_clicked();
	void on_pushButton_shiftLanguage_clicked();

	void showBatteryInfo();
	void showBatteryDetailInfo();
	void showProtectParamInfo(ProtectParamInfo protectParamInfo);
	void showInitFactoryParamInfo(FactoryParamInfo factoryParamInfo);

	void protectParamChanged();
	void factoryParamChanged();

	void shiftTabWidget(int index);
	void shiftStationAddress(int index);

	void startTimer();
	void connectSuccess();

protected:
	void downsendDataWithPos();

	void closeEvent(QCloseEvent *event) override {
		// 禁用 QLineEdit
		//ui->lineEdit_UART1_ADDR->disconnect();
		QList<QLineEdit*> lineEdits = this->findChildren<QLineEdit*>();
		for (QLineEdit* lineEdit : lineEdits) {
			QObject::disconnect(lineEdit, SIGNAL(editingFinished()), 0, 0);
		}
		// 执行窗口关闭的默认操作
		event->accept();
	}

private:
	void init();
	void initVerifyDialog();
	void initMoreInfoDialog();
	void initCommunicator();
	void initUIData();
	void initProtectParamTabWidget();
	void initProtectParamTabWidgetSignalsAndSlots();
	void initFactoryParamTabWidgetSiganlSAndSlots();

	bool checkIdentification();

	void setChinese();
	void setEnglish();

private:
    Ui::LiBatteryManagerSysClass *ui;
	VerifyDialog *m_verifyDialog;
	MoreInfoDialog *m_moreInfoDialog;
	Communicator *m_communicator;
	QTimer m_timer;
	QTimer m_updateTimer;
	int m_updateTime = 0;
	QMutex m_mutex;
	QThread *m_communicateThread;
	QVector<QTableWidgetItem*> m_numItemVec;
	QVector<QTableWidgetItem*> m_vItemVec;
	QVector<QTableWidgetItem*> m_tItemVec;
	int m_lastIndex = 0;

	bool activeState = false;
	bool m_isManager = false;
	bool m_isChinese = true;
};
